<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
/*
 * Created on 26-07-2008
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 class BWebshopSpecialProductBean
 {
 	//constructor specials
 	function BWebshopSpecialProductBean()
 	{
 		
 	}
 	/*
 	 * Validate product data
     */
    function validateSpecial()
    {
    	$msg = "";	
    
    	if(trim($_POST['product_id']) == 0)
    	{
    		$msg = _("You must select a product"); 
    	}
    	
		if(trim($_POST['specials_new_products_price']) == "")
		{
			if($msg != "") $msg .= "<br />";
			$msg .= _("You must enter the new price");
		}
		
		if(trim($_POST['specials_new_products_price']) != ""&&is_numeric($_POST['specials_new_products_price']) == false)
		{
			if($msg != "") $msg .= "<br />";
			$msg .= _("Invalid new price, new price should be a numeric value.");
		}
		
		return $msg;
    }
       
       
 	function getAllSpecials($db,$sort,$by) {
 		if($sort!="") {
			if($sort=='product_group_name')
				$condsort = "ORDER BY g.".$sort." ".$by;
			else
				$condsort = "ORDER BY p.".$sort." ".$by;
    	} else {
			$condsort = "ORDER BY s.position ";
    	}
		$strSQL = " SELECT g.product_group_name,p.product_id, p.product_name, p.product_price, p.product_thumbnail,s.position, s.specials_new_products_price, s.status, s.special_id, c.symbol_left ".
		 			" FROM ". TBL_SPECIAL ." s  " .
					"LEFT JOIN ". TBL_PRODUCT . " p ON s.product_id = p.product_id ".
					"LEFT JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id ".
					"LEFT JOIN ".TBL_PRODUCT_GROUP." g ON p.product_group_id = g.product_group_id ".
					" WHERE p.product_id = s.product_id $condsort";
		
		$res = $db->get_results($strSQL);
		return $res;
	}
	
	function getAllSpecialsbyStatus($db)
	{
		$strSQL = " SELECT p.product_id, p.product_name, p.product_price, p.product_thumbnail, s.specials_new_products_price, s.status, s.special_id, c.symbol_left";
		$strSQL = $strSQL . " FROM ". TBL_PRODUCT ." p, " . TBL_SPECIAL . " s " . " JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id";
		$strSQL = $strSQL . " WHERE (p.product_id = s.product_id AND s.status = 1) ORDER BY s.special_id ASC";
		
		$res = $db->get_results($strSQL);
		return $res;
	}
	
	function getAllSpecialsSharePage($db)
	{
		$page = isset ( $_GET["list"] ) ? intval ( $_GET["list"] ) : 1;
		$rows_per_page= MAX_DEMO_PRODUCT;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
	
		$strSQL = " SELECT g.product_group_name,p.product_id, p.product_name, p.product_price, p.product_thumbnail, s.specials_new_products_price, s.special_id, c.symbol_left ".
		 			" FROM ". TBL_PRODUCT ." p, " . TBL_SPECIAL . " s " . " JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id ".
					"LEFT JOIN ".TBL_PRODUCT_GROUP." g ON p.product_group_id = g.product_group_id ".
				 " WHERE p.product_id = s.product_id AND s.status = '1' ORDER BY s.special_id ASC LIMIT $page_start, $rows_per_page";
		
		$res = $db->get_results($strSQL);
		return $res;
	}
	/*
	 * Get special by specialId
	 */
	function getSpecialBySpecialId($db, $id)
	{
		$strSQL = "SELECT s.*, DATE_FORMAT(s.expires_date, '%d.%m.%Y') as expires_date,p.product_thumbnail, p.product_name, p.product_price, c.symbol_left FROM " . TBL_PRODUCT . " p, " . TBL_SPECIAL . " s".
					" JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id WHERE (p.product_id = s.product_id AND s.special_id = $id)";
		
		$res = $db->get_row($strSQL);
		return $res;
	}	
	
	/*
	 * Get special by specialId
	 */
	function getSpecialByProductId($db, $id)
	{
		$strSQL = "SELECT s.*, p.*  FROM " . TBL_PRODUCT . " p, " . TBL_SPECIAL . " s WHERE (p.product_id = s.product_id AND p.product_id = $id)";
		
		$res = $db->get_row($strSQL);
		return $res;
	}
	
	function getSpecialStatusById($db, $Id)  
    {
     	$strSQL = " SELECT date_status_change, status FROM " . TBL_SPECIAL . " WHERE special_id = $Id";
     	$res = $db->get_row($strSQL);
		return $res;
    }	
	
	/*
	 * Get special by category Id
	 */
	function getSpecialByCategoryId($db,$catId)
	{
		$strSQL = "SELECT s.*, p.*, c.symbol_left FROM " . TBL_PRODUCT . " p, " . TBL_SPECIAL . " s JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id".
					" WHERE (p.product_id = s.product_id AND s.status = 1 AND p.category_id = $catId)";
		$res= $db->get_results($strSQL);
		return $res;
	}
	
	function getSpecialByCategoryIdSharePage($db,$catId)
	{
		$page = isset ( $_GET["list"] ) ? intval ( $_GET["list"] ) : 1;
		$rows_per_page= MAX_DEMO_PRODUCT;
		$page_start = ( $page - 1 ) * $rows_per_page;
		$page_end = $page * $rows_per_page;
		$strSQL = "SELECT s.*, p.*, c.symbol_left FROM " . TBL_PRODUCT . " p, " . TBL_SPECIAL . " s JOIN " . TBL_CURRENCY . " c ON s.currency_id = c.currency_id".
					" WHERE (p.product_id = s.product_id AND p.category_id = $catId AND s.status = 1) LIMIT $page_start, $rows_per_page";
		$res= $db->get_results($strSQL);
		return $res;
	}
	
	
	/* Author	:
 	 * Desc		: Save array specials to database
 	 * Param	:
 	 * 			@$db :
 	 * 			@special  :
 	 */
	function saveSpecial($db,$special)
    {
    	$sql=sprintf(
					"INSERT INTO %s(product_id, currency_id, specials_new_products_price,specials_date_added, expires_date, position, date_status_change,status) " .
					"VALUES('%d','%d','%s',now(),'%s','%s','%s','%s')",
    				TBL_SPECIAL, 
    				$special['product_id'],
    				$special['currency_id'],
    				$special['specials_new_products_price'],
					$special['expires_date'],
    				$special['position'],
    				$special['date_status_change'],
    				($special['status'] != "" ? "1" : "0"));  
        		
    	$res = $db->query($sql);
    	
    	if($res == true)
    		return $db->insert_id;
    	else
    		return -1;
    	
    }
	
	/* Author	:
 	 * Desc		: Update array specials to database
 	 * Param	:
 	 * 			@$db :
 	 * 			@special  :
 	 */
	function updateSpecial($db,$special)
    {
    	$sql = sprintf("UPDATE  %s SET product_id = '%d'," .
    					" currency_id = '%d'," .
    					" specials_new_products_price = '%s'," .
    					" specials_last_modified = now()," .
    					" expires_date = '%s'," .
    					" date_status_change = '%s'," .
    					" status = '%s'" .
    					" WHERE special_id = '%d'", 
					TBL_SPECIAL,
					$special['product_id'], 
					$special['currency_id'],
					$special['specials_new_products_price'],
					$special['expires_date'],
					$special['date_status_change'],
					($special['status'] != "" ? "1" : "0"),
					$special['special_id']);
		$res = $db->query($sql);
    	return $res;
    }
 	/* Author	:
 	 * Desc		: Delete specials items based on $id
 	 * Param	:
 	 * 			@$db :
 	 * 			@id  :
 	 */
	function deleteSpecial($db, $id)
    {
    	$sql = sprintf("DELETE FROM %1\$s" .
				" where %1\$s.special_id = '$id'", 
				TBL_SPECIAL);
		
    	$res = $db->query($sql);
    	return $res;
    }
	
	/*
	 * Get list special product to change position 
	 */
	
	public static function get_special_by_level($db,$nowid) {
    	$strSQL = sprintf("SELECT s.*,p.product_name FROM ".TBL_SPECIAL." s " .
				"LEFT JOIN " . TBL_PRODUCT . " p ON p.product_id = s.product_id ".
				" WHERE s.special_id != '$nowid' ORDER BY position ");
    	$res = $db->get_results($strSQL);
    	return $res;
    }
	
	/* Input: 	- id and value position active of special product need set position
	
	*/
	
	 public static function update_position_special($db, $id,$value) {
    	$strSQL = sprintf("UPDATE  %s SET position ='%d' " .
    					"WHERE special_id = '%d'", 
					TBL_SPECIAL,
					$value,
					$id);
		$res = $db->query($strSQL);

    	return $res;
    }
 	
 } 
?>
